package com.jmm.common.utils;

import cn.hutool.log.StaticLog;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.jmm.common.config.AliDyConfig;
import com.jmm.common.constant.AliDySmsTemplateCodeConstants;

import java.util.HashMap;
import java.util.Map;

/**
 * 阿里大鱼发送短信接口
 */
public class AliDySmsUtils {

    /**
     * 短信发送方法
     *
     * @param phoneNum 发送人手机号
     * @param code     发送短信模板code
     * @param params   发送内容Map 可为Null
     * @return 返回短信发送响应
     */
    public static SendSmsResponse sendSms(String phoneNum, String code, Map<String, String> params) {
        StaticLog.info("执行发送：{},模版为：{},参数为：{}",phoneNum,code,JSON.toJSONString(params));
        SendSmsResponse sendSmsResponse = null;
        try {
            //可自助调整超时时间
            System.setProperty("sun.net.client.defaultConnectTimeout", "5000");
            System.setProperty("sun.net.client.defaultReadTimeout", "5000");

            //初始化acsClient,暂不支持region化
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", AliDyConfig.accessKeyId, AliDyConfig.accessKeySecret);
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", AliDyConfig.product, AliDyConfig.domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);

            //组装请求对象-具体描述见控制台-文档部分内容
            SendSmsRequest request = new SendSmsRequest();
            //必填:待发送手机号
            StaticLog.info("大于短信发送手机号:{}",phoneNum);
            request.setPhoneNumbers(phoneNum);
            //必填:短信签名-可在短信控制台中找到
            request.setSignName(AliDyConfig.signName);
            //必填:短信模板-可在短信控制台中找到
            request.setTemplateCode(code);
            //可选:模板中的变量替换JSON串
            if (params != null) {
                String paramsString = JSON.toJSONString(params);
                StaticLog.info("大于短信发送主体:{}",paramsString);
                request.setTemplateParam(paramsString);
            }

            //选填-上行短信扩展码(无特殊需求用户请忽略此字段)
            //request.setSmsUpExtendCode("90997");

            //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
            //request.setOutId("yourOutId");

            //hint 此处可能会抛出异常，注意catch
            sendSmsResponse = acsClient.getAcsResponse(request);
            StaticLog.info("短信发送回调\n--->{}\n", JSON.toJSONString(sendSmsResponse));
        } catch (ClientException e) {
            StaticLog.error("大于短信调用失败:{}",e.toString());
        } catch (Exception e){
            StaticLog.error("大于短信异常:{}",e.toString());
        }
        return sendSmsResponse;
    }

    /**
     * The entry point of application.
     *
     * @param args the input arguments
     */
    public static void main(String[] args) {
        HashMap<String, String> stringStringHashMap = new HashMap();
        stringStringHashMap.put("pwdverification","1234");
        SendSmsResponse sendSmsResponse = sendSms("15396888975", AliDySmsTemplateCodeConstants.SMS_53510049, stringStringHashMap);
    }
}
